/**
 * com.chaeron.GPS - An API for access to GPS devices.
 *
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 * @version     1.02
 * @author      Andrzej Jan Taramina, Chaeron Corporation
 *              andrzej@chaeron.com
 */

package com.chaeron.GPS.event.decorator;

//***************************************************************************
//*
//*     Imports
//*
//***************************************************************************/

import com.chaeron.GPS.*;
import com.chaeron.GPS.event.*;

import com.chaeron.platform.common.*;


//***************************************************************************
//*
//*    GPSRouteDecorator Class definition
//*
//***************************************************************************/

/**
 * The GPSRouteDecorator provides added functions to the GPSRoute Event Value
 *
 */
 
public class GPSRouteDecorator extends GPSEventDecorator implements GPSRoute
{

	//***************************************************************************
	//*
	//*     Constant definitions
	//*
	//***************************************************************************/
	
	//***************************************************************************
	//*
	//*     Attributes
	//*
	//***************************************************************************/
	
	private	GPSRoute			delegate	= null;
	

	//***************************************************************************
	//*
	//*     Constructor
	//*
	//***************************************************************************/
	
	/**
	 * Create a GPSRouteDecorator
	 *
	 * @param	route	GPSTrackLog to delegate method calls to
	 */
	
	public GPSRouteDecorator( GPSRoute route )
	{
		super();
		
		delegate = route;
	}
	
	//***************************************************************************
	//*
	//*     Output Methods (required by abstract base class)
	//*
	//***************************************************************************/
	
	void toXMLImpl( boolean namespace )
	{
		if( Compile.INCLUDE_EVENT_TOXML ) {
			writeString( genXMLStartTag( GPSXMLTag.XML_TAG_ROUTE, namespace ) );
			
			if( isMethodImplemented( ROUTE_METHOD_NUMBER ) ) {
				writeString( genXMLValueTag( GPSXMLTag.XML_TAG_NUMBER, String.valueOf( delegate.getNumber() ) ) );
			}
			
			if( isMethodImplemented( ROUTE_METHOD_IDENTIFICATION ) ) {
				writeString( genXMLValueTag( GPSXMLTag.XML_TAG_IDENTIFIER, delegate.getIdentification() ) );
			}

			if( isMethodImplemented( ROUTE_METHOD_COMMENT ) ) {
				writeString( genXMLValueTag( GPSXMLTag.XML_TAG_COMMENT, delegate.getComment() ) );
			}
			
			childrenToXML();

			writeString( genXMLEndTag( GPSXMLTag.XML_TAG_ROUTE ) ) ;
		}
	}
	
	
	private void childrenToXML()
	{
		Vector						waypoints		= delegate.getWaypoints();
		GPSWaypointDecorator		waypoint		= null;
		Vector						links			= delegate.getLinkData();
		GPSRouteLinkDataDecorator	linkData		= null;
		int							count			= 0;
		boolean						includeWaypts	= false;
		boolean						includeLinks	= false;
		
		if( isMethodImplemented( ROUTE_METHOD_WAYPOINTS ) && waypoints != null ) {
			includeWaypts = true;
		} 
		
		if( isMethodImplemented( ROUTE_METHOD_LINKDATA ) && links != null ) {
			includeLinks = true;
		}

		if( includeWaypts ) {
			count = waypoints.getCount();
			
			for( int i = 0; i < count; i++ ) {
				waypoint = (GPSWaypointDecorator)waypoints.get( i );
			
				writeString( waypoint.childToXML( getOutCatalog() ) );
			}
		}
		
		if( includeLinks && links.getCount() > 0 ) {
			count = links.getCount();
			
			writeString( genXMLStartTag( GPSXMLTag.XML_TAG_OTHER, false ) );
			
			for( int i = 0; i < count; i++ ) {
				linkData = (GPSRouteLinkDataDecorator)links.get( i );
				
				writeString( linkData.childToXML( getOutCatalog() ) );
			}
			
			writeString( genXMLEndTag( GPSXMLTag.XML_TAG_OTHER ) );
		}
	}

	
	//***************************************************************************
	//*
	//*     Delegated Interface Methods
	//*
	//***************************************************************************/

	public byte getNumber()
	{
		return( delegate.getNumber() );
	}

	
	public String getIdentification()
	{
		return( delegate.getIdentification() );
	}
	
	
	public String getComment()
	{
		return( delegate.getComment() );
	}
	
	
	public Vector getWaypoints()
	{
		return( delegate.getWaypoints() );
	}

	
	public Vector getLinkData()
	{
		return( delegate.getLinkData() );
	}
	
}
